package hai.nguyenvan;

import hai.nguyenvan.service.DatabaseConnection;
import hai.nguyenvan.service.Validation;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.HeadlessException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;

import java.awt.Font;
import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.swing.JOptionPane;
import javax.swing.JSeparator;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JTextField;
import javax.swing.ImageIcon;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.JPasswordField;

public class ConnectionFrame extends JFrame {

	private JPanel contentPane;
	private JTextField txtIP;
	private JTextField txtPort;
	private JTextField txtService;
	private JTextField txtUser;
	private BufferedImage image;
	private JCheckBox cbSave;
	private JPasswordField txtPassword;
	private MainFrame mainFrame;
	private DatabaseConnection connection;

	public DatabaseConnection getConnection() {
		return connection;
	}

	public void setConnection(DatabaseConnection connection) {
		this.connection = connection;
	}

	public MainFrame getMainFrame() {
		return mainFrame;
	}

	public void setMainFrame(MainFrame mainFrame) {
		this.mainFrame = mainFrame;
	}

	/**
	 * Create the frame.
	 */
	public ConnectionFrame(MainFrame mainFrame) {
		this.mainFrame = mainFrame;
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 382, 352);
		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JLabel lblDatabaseInformation = new JLabel("Database Information");
		lblDatabaseInformation.setFont(new Font("Times New Roman", Font.BOLD,
				20));
		lblDatabaseInformation.setBounds(10, 0, 346, 25);
		contentPane.add(lblDatabaseInformation);

		JSeparator separator = new JSeparator();
		separator.setBounds(10, 33, 346, 2);
		contentPane.add(separator);

		JPanel panel = new JPanel();
		panel.setBounds(10, 46, 346, 191);
		contentPane.add(panel);
		panel.setLayout(null);

		JLabel lblIpAddress = new JLabel("IP Address");
		lblIpAddress.setEnabled(false);
		lblIpAddress.setBounds(24, 11, 91, 14);
		panel.add(lblIpAddress);

		txtIP = new JTextField();
		txtIP.setEnabled(false);
		txtIP.setBounds(153, 8, 183, 20);
		panel.add(txtIP);
		txtIP.setColumns(10);

		JLabel lblPort = new JLabel("Port");
		lblPort.setEnabled(false);
		lblPort.setBounds(24, 47, 91, 14);
		panel.add(lblPort);

		txtPort = new JTextField();
		txtPort.setEnabled(false);
		txtPort.setBounds(153, 44, 183, 20);
		panel.add(txtPort);
		txtPort.setColumns(10);

		JLabel lblService = new JLabel("Service");
		lblService.setEnabled(false);
		lblService.setBounds(24, 82, 91, 14);
		panel.add(lblService);

		txtService = new JTextField();
		txtService.setEnabled(false);
		txtService.setBounds(153, 79, 183, 20);
		panel.add(txtService);
		txtService.setColumns(10);

		JLabel lblUser = new JLabel("User");
		lblUser.setBounds(24, 122, 91, 14);
		panel.add(lblUser);

		txtUser = new JTextField();
		txtUser.setBounds(153, 119, 183, 20);
		panel.add(txtUser);
		txtUser.setColumns(10);

		JLabel lblPassword = new JLabel("Password");
		lblPassword.setBounds(24, 160, 91, 14);
		panel.add(lblPassword);

		txtPassword = new JPasswordField();
		txtPassword.setBounds(153, 157, 183, 20);
		panel.add(txtPassword);

		JButton btnConnect = new JButton("Connect");
		btnConnect.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				getDataFromForm();
			}
		});
		btnConnect.setBounds(260, 274, 96, 23);
		contentPane.add(btnConnect);

		JButton btnClear = new JButton("Clear");
		btnClear.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				clearData();
			}
		});
		btnClear.setBounds(181, 274, 79, 23);
		contentPane.add(btnClear);

		JButton btnClose = new JButton("Close");
		btnClose.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (!cbSave.isSelected()) {
					clearData();
				}
				setVisible(false);
				if (MainFrame.mnConnect.isSelected()) {
					MainFrame.mnConnect.setSelected(false);
				}
			}
		});
		btnClose.setBounds(103, 274, 79, 23);
		contentPane.add(btnClose);

		cbSave = new JCheckBox("Save Information");
		cbSave.setEnabled(false);
		cbSave.setSelected(true);
		cbSave.setBounds(223, 244, 133, 23);
		contentPane.add(cbSave);

		JLabel lblNewLabel = new JLabel("");
		lblNewLabel.setBounds(20, 242, 73, 60);
		contentPane.add(lblNewLabel);

		try {
			image = ImageIO.read(ConnectionFrame.class
					.getResource("/hai/nguyenvan/images/icon.JPG"));
			MainFrame.setPicture(lblNewLabel, image);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private void getDataFromForm() {
		// TODO Auto-generated method stub
		String IPAdress = txtIP.getText();
		String port = txtPort.getText();
		String service = txtService.getText();
		String user = txtUser.getText();
		String password = txtPassword.getText();

		if (user.equals("") || password.equals("")) {
			JOptionPane.showMessageDialog(null, "Fill full Information");
			return;
		}
//		if (Validation.isIPAddress(IPAdress) == false) {
//			JOptionPane.showMessageDialog(null, "IPAddress error");
//			return;
//		}
//		if (checkPort(port) == false) {
//			JOptionPane.showMessageDialog(null, "Port error");
//			return;
//		}
		connection = new DatabaseConnection();
		String result = connection.connectDatabase(user, password);
		if (result.equals("Connected Successfull")) {
			getMainFrame().loadTable();
		} else {
			JOptionPane.showMessageDialog(null, "Error");
		}

	}

//	private boolean checkPort(String port) {
//		// TODO Auto-generated method stub
//		boolean check = true;
//		try {
//			int k = -1;
//			k = Integer.parseInt(port);
//			if (k < 0) {
//				return false;
//			}
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			return false;
//		}
//		return check;
//	}

	private void clearData() {
		txtIP.setText("");
		txtPort.setText("");
		txtService.setText("");
		txtUser.setText("");
		txtPassword.setText("");
		cbSave.setEnabled(false);
	}

}
